package com.mapper;


import com.pojo.GroupJoin;
import com.pojo.GroupJoinVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GroupJoinMapper {
    @Insert("INSERT INTO group_join(user_id, activity_id, join_time) VALUES(#{userId}, #{activityId}, NOW())")
    void i(Long userId, Long activityId);


    @Insert("INSERT INTO group_join (activity_id, user_id, joined_at, is_initiator) " +
            "VALUES (#{activityId}, #{userId}, #{joinedAt}, #{isInitiator})")
    int insert(GroupJoin join);
    @Select("""
    SELECT 
        gj.id,
        gj.joined_at,
        gj.is_initiator,
        gj.activity_id, -- 加这个
        ga.group_size,
        (
            SELECT COUNT(*) 
            FROM group_join gj2 
            WHERE gj2.activity_id = ga.id
        ) AS current_size,
        p.id AS product_id,
        p.name,
        p.description,
        p.price,
        p.stock,
        p.image_url,
        p.category_id,
        p.status,
        p.created_at
    FROM group_join gj
    LEFT JOIN group_activity ga ON gj.activity_id = ga.id
    LEFT JOIN product p ON ga.product_id = p.id
    WHERE gj.user_id = #{userId}
    ORDER BY gj.joined_at DESC
""")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "joinedAt", column = "joined_at"),
            @Result(property = "isInitiator", column = "is_initiator"),
            @Result(property = "activityId", column = "activity_id"),  // 新增映射
            @Result(property = "groupSize", column = "group_size"),
            @Result(property = "currentSize", column = "current_size"),
            @Result(property = "product.id", column = "product_id"),
            @Result(property = "product.name", column = "name"),
            @Result(property = "product.description", column = "description"),
            @Result(property = "product.price", column = "price"),
            @Result(property = "product.stock", column = "stock"),
            @Result(property = "product.imageUrl", column = "image_url"),
            @Result(property = "product.categoryId", column = "category_id"),
            @Result(property = "product.status", column = "status"),
            @Result(property = "product.createdAt", column = "created_at"),
    })
    List<GroupJoinVO> selectByUserIdWithActivity(@Param("userId") Long userId);



    @Select("""
                SELECT gj.* FROM group_join gj
                WHERE gj.activity_id = #{activityId}
                AND gj.is_initiator = true
                AND (
                    SELECT COUNT(*) FROM group_join WHERE activity_id = #{activityId}
                ) < (
                    SELECT group_size FROM group_activity WHERE id = #{activityId}
                )
                ORDER BY gj.joined_at ASC
            """)
    List<GroupJoin> findOpenGroupsByActivityId(@Param("activityId") Long activityId);

    @Select("SELECT * FROM group_join WHERE activity_id = #{activityId} AND user_id = #{userId} LIMIT 1")
    GroupJoin findByActivityIdAndUserId(@Param("activityId") Long activityId,
                                        @Param("userId") Long userId);


}
